package user_api

import (
	"context"
	"gin_study_blog/common"
	"gin_study_blog/common/http"
	"gin_study_blog/utils"
	"time"

	"github.com/gin-gonic/gin"
)

// UserLogout 注销
func (UserApi) UserLogout(c *gin.Context) {
	claims, _ := c.Get("user")
	user := claims.(*utils.UserClaims)

	// 计算距离过期时间多长
	exp := user.ExpiresAt
	now := time.Now()
	diff := exp.Time.Sub(now)

	// 初始化 redis 将退出的 token 保存在redis当中
	token := c.GetHeader("token")
	//redisClient := core.InitRedis()
	//err := redisClient.Set("logout_"+token, token, diff)

	err := common.Redis.Set(context.Background(), "logout_"+token, token, diff).Err()
	if err != nil {
		common.Log.Error(err.Error())
		http.FailWithMessage("注销失败", c)
		return
	}
	http.IsOkWithMessage("注销成功", c)
}
